package com.app.aipass.fragment.my.exchangepoints.exchangepointsrecord;

import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import com.app.aipass.BR;
import com.app.aipass.R;
import com.app.aipass.app.AppViewModelFactory;
import com.app.aipass.databinding.FragmentExchangepointsRecordTabBinding;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import me.goldze.mvvmhabit.base.BaseFragment;
import me.tatarka.bindingcollectionadapter2.BindingRecyclerViewAdapter;

/**
 * @author: woking
 * @date: 2021/6/10
 * @description：
 */
public class ExchangePointsRecordTabFragment extends BaseFragment<FragmentExchangepointsRecordTabBinding, ExchangePointsRecordViewModel> {
    private String type = "AD";
    public static ExchangePointsRecordTabFragment newInstance(String type) {
        ExchangePointsRecordTabFragment fragment = new ExchangePointsRecordTabFragment();
        Bundle args = new Bundle();
        args.putString("Type", type);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void initParam() {
        super.initParam();
        getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }

    //初始化根布局
    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_exchangepoints_record_tab;
    }

    //初始化ViewModel的id
    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    //初始化ViewModel
    @Override
    public ExchangePointsRecordViewModel initViewModel() {
        //使用自定义的ViewModelFactory来创建ViewModel，如果不重写该方法，则默认会调用NetWorkViewModel(@NonNull Application application)构造方法
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getActivity().getApplication());
        return ViewModelProviders.of(this, factory).get(ExchangePointsRecordViewModel.class);
    }

    @Override
    public void initData() {
        if (getArguments() != null) {
            type = getArguments().getString("Type");
            viewModel.isAD.setValue(type);
        }

        binding.myrecyclerview.setEmptyView(binding.emptyView);

        //给RecyclerView添加Adpter，请使用自定义的Adapter继承BindingRecyclerViewAdapter，重写onBindBinding方法，里面有你要的Item对应的binding对象。
        // Adapter属于View层的东西, 不建议定义到ViewModel中绑定，以免内存泄漏
        binding.setAdapter(new BindingRecyclerViewAdapter());
//        if(!type)
//        {
//            viewModel.getShopToken();
//        }
        //请求网络数据
        viewModel.requestNetWork(false);
        showDialog("");

        initRefreshLayout();
    }
    private void initRefreshLayout()
    {
        binding.twinklingRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                viewModel.onRefresh();
            }
        });
//        if(!type)
//        {
//            binding.twinklingRefreshLayout.setEnableLoadMore(false);
//        }
        binding.twinklingRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                viewModel.onLoadMore();
            }
        });
//        binding.twinklingRefreshLayout.autoRefresh();
    }

//    private void initRefreshLayout()
//    {
//        MySinaRefreshView headerView = new MySinaRefreshView(getActivity());
//        headerView.setTextColor(0xff745D5C);
//        binding.twinklingRefreshLayout.setHeaderView(headerView);
////        SwipeRefreshLayout
//
//        //是否需要下拉刷新,默认需要true
//        binding.twinklingRefreshLayout.setEnableRefresh(true);
//
//        //是否需要加载更多,默认需要true
//        binding.twinklingRefreshLayout.setEnableLoadmore(true);
//
//        //是否自动加载更多, 滑到底部默认加载更多,默认false
//        binding.twinklingRefreshLayout.setAutoLoadMore(true);
//
//        //如果你想进入到界面的时候主动调用下刷新，可以调用startRefresh()/startLoadmore()方法。
////        binding.twinklingRefreshLayout.startRefresh();
//        // mBinding.refreshLayout.startLoadMore();
//
//        //是否允许进入越界回弹模式,默认true
//        binding.twinklingRefreshLayout.setEnableOverScroll(true);
//
//        //是否开启悬浮刷新模式(支持切换到像SwipeRefreshLayout一样的悬浮刷新模式),默认false
//        binding.twinklingRefreshLayout.setFloatRefresh(true);
//
//        //TwinklingRefreshLayout不会自动结束刷新或者加载更多，需要手动控制
//        binding.twinklingRefreshLayout.setOnRefreshListener(new RefreshListenerAdapter() {
//            @Override
//            public void onRefresh(TwinklingRefreshLayout refreshLayout) {
//                super.onRefresh(refreshLayout);
//                viewModel.onRefresh();
////                new Handler().postDelayed(() -> {
////                    refreshLayout.finishRefreshing();
////                    viewModel.dismissDialog();
////                }, 1000);
//            }
//
//            @Override
//            public void onLoadMore(TwinklingRefreshLayout refreshLayout) {
//                super.onLoadMore(refreshLayout);
//                viewModel.onLoadMore();
////                new Handler().postDelayed(() -> {
////                    refreshLayout.finishLoadmore();
////                    viewModel.dismissDialog();
////                }, 1000);
//            }
//        });
//    }
    ////页面事件监听的方法，一般用于ViewModel层转到View层的事件注册
    @Override
    public void initViewObservable() {
        //监听下拉刷新完成
        viewModel.uc.finishRefreshing.observe(this, new Observer() {
            @Override
            public void onChanged(@Nullable Object o) {
                //结束刷新
                binding.twinklingRefreshLayout.finishRefresh();
//                viewModel.dismissDialog();
                dismissDialog();
            }
        });
        //监听上拉加载完成
        viewModel.uc.finishLoadmore.observe(this, new Observer() {
            @Override
            public void onChanged(@Nullable Object o) {
                //结束刷新
                binding.twinklingRefreshLayout.finishLoadMore();
//                viewModel.dismissDialog();
            }
        });
//        //监听删除条目
//        viewModel.deleteItemLiveData.observe(this, new Observer<ExchangePointsRecordTabViewModel>() {
//            @Override
//            public void onChanged(ExchangePointsRecordTabViewModel netWorkItemViewModel) {
//                int index = viewModel.getItemPosition(netWorkItemViewModel);
//                //删除选择对话框
//                MaterialDialogUtils.showBasicDialog(getContext(), "提示", "是否删除【" + netWorkItemViewModel.entity.get().getName() + "】？ position：" + index)
//                        .onNegative(new MaterialDialog.SingleButtonCallback() {
//                            @Override
//                            public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
//                                ToastUtils.showShort("取消");
//                            }
//                        }).onPositive(new MaterialDialog.SingleButtonCallback() {
//                    @Override
//                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
//                        viewModel.deleteItem(netWorkItemViewModel);
//                    }
//                }).show();
//            }
//        });
    }
}